<template>
    <el-aside :width="width">
        <el-menu 
        background-color="#545c64"
        text-color="#fff"
        :collapse="isCollapse"
        :collapse-transition="false"
        :default-active="activeMenu"
        >
            <h3 v-show="!isCollapse">博客后台管理系统</h3>
            <h3 v-show="isCollapse">后台</h3>
            <el-menu-item v-for="item in noChildren" :index="item.path" :key="item.path" @click="handleMenu(item)">
                <component class="icons" :is="item.icon"></component>
                <span>{{ item.label }}</span>
            </el-menu-item>
            <el-sub-menu v-for="item in hasChildren" :index="item.path" :key="item.path">
                <template #title>
                    <component class="icons" :is="item.icon"></component>
                    <span>{{ item.label }}</span>
                </template>
                <el-menu-item-group>
                    <el-menu-item 
                    v-for="subItem in item.children" 
                    :index="subItem.path" 
                    :key="subItem.path"
                    @click="handleMenu(subItem)">
                    <component class="icons" :is="subItem.icon">
                    </component>
                    <span>{{ subItem.label }}</span>
                    </el-menu-item>
                </el-menu-item-group>
            </el-sub-menu>
            <el-menu-item :index="blogItem.path" @click="handleMenu(blogItem)">
                <component class="icons" :is="blogItem.icon"></component>
                <span>{{ blogItem.label }}</span>
            </el-menu-item>
        </el-menu>
    </el-aside>
</template>
<script setup>
import { ref, computed } from 'vue'
import {useStore} from '@/store/index.js'
import { useRouter,useRoute } from 'vue-router';
const router = useRouter()
const route = useRoute()
const store = useStore()
const activeMenu = computed(()=>{
    return route.path
})
const handleMenu = (item) => {
    if (item.path === 'http://localhost:8090' || item.path.startsWith('http')) {
        window.open(item.path, '_blank');
    } else {
        router.push(item.path);
        store.addTags(item);
    }
}
const list = ref([
    {
        path: '/home',
        name: 'home',
        label: '首页',
        icon: 'house',
        url: 'Home'
    },
    {
        path: '/system',
        label: '系统管理',
        icon: 'setting',
        children: [
            {
                path: '/system/user',
                name: 'user',
                label: '用户管理',
                icon: 'user',
                url: 'Users'
            },
            {
                path: '/system/role',
                name: 'role',
                label: '角色管理',
                icon: 'key',
                url: 'Roles'
            }
        ]
    },
    {
        path: '/content',
        label: '内容管理',
        icon: 'document',
        children: [
            {
                path: '/content/article',
                name: 'article',
                label: '文章管理',
                icon: 'document',
                url: 'Articles'
            },
            {
                path: '/content/tag',
                name: 'tag',
                label: '标签管理',
                icon: 'price-tag',
                url: 'Tags'
            },
            {
                path: '/content/comment',
                name: 'comment',
                label: '评论管理',
                icon: 'chat-dot-round',
                url: 'Comments'
            }
        ]
    },
    {
        path: '/tool',
        label: '工具管理',
        icon: 'tools',
        children: [
            {
                path: '/tool/center',
                name: 'center',
                label: '工具中心',
                icon: 'box',
                url: 'Tools'
            },
            {
                path: '/tool/category',
                name: 'category',
                label: '工具分类',
                icon: 'folder',
                url: 'Categories'
            }
        ]
    },
    {
        path: '/business',
        label: '业务运营',
        icon: 'office-building',
        children: [
            {
                path: '/business/project',
                name: 'project',
                label: '项目管理',
                icon: 'files',
                url: 'Projects'
            },
            {
                path: '/business/category',
                name: 'projectcat',
                label: '项目分类',
                icon: 'folder-opened',
                url: 'ProjectCats'
            },
            {
                path: '/business/lead',
                name: 'lead',
                label: '意向跟进',
                icon: 'phone',
                url: 'Leads'
            }
        ]
    },
    {
        path: '/log',
        label: '日志管理',
        icon: 'tickets',
        children: [
            {
                path: '/log/syslog',
                name: 'syslog',
                label: '系统日志',
                icon: 'warning',
                url: 'SysLog'
            },
            {
                path: '/log/oplog',
                name: 'oplog',
                label: '操作日志',
                icon: 'data-analysis',
                url: 'OpLog'
            }
        ]
    }
])

// 添加单独处理博客前台的菜单项
const blogItem = {
    path: 'https://giaocode.cn',
    name: 'blog',
    label: '博客前台',
    icon: 'monitor',
    url: 'Blog'
}

const noChildren = computed(() => {
    return list.value.filter(item => {
        return !item.children && item.path === '/home'
    })
})
const hasChildren = computed(() => {
    return list.value.filter(item => {
        return item.children
    })
})

const isCollapse = computed(()=>{
    return store.state.isCollapse
})  
const width = computed(()=>{
   return store.state.isCollapse ? "64px" : "180px"
})



</script>
<style lang="less" scoped>
.el-menu-item-group__title{
    padding: 0 !important;
    height: 0 !important;
    font-size: 0 !important;
    display: none !important;
}

:deep(.el-sub-menu .el-menu-item-group) {
    padding: 0 !important;
}

:deep(.el-menu-item-group) {
    padding-top: 0 !important;
    margin-top: 0 !important;
}

:deep(.el-menu-item-group__title) {
    padding: 0 !important;
    height: 0 !important;
    font-size: 0 !important;
    display: none !important;
}
.icons{
    width: 18px;
    height: 18px;
    margin-right: 5px;
}
.el-menu{
    border-right: none;
    h3{
        line-height: 48px;
        color: #fff;
        text-align: center;
        
    }
}
.el-aside{
    height: 100%;
    background-color: #545c64;
}


</style>